using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Eblah_Template_Editor_CSharp
{
    public partial class frmMain : Form
    {
        bool docopen = false;
        OpenFileDialog openFileDialog = new OpenFileDialog();
        private int childFormNumber = 0;
        private frmDocument document;

        public frmMain()
        {
            //frmDocument.
            InitializeComponent();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            docopen = true;
            // Create a new instance of the child form.
            Form childForm = new frmDocument();
            // Make it a child of this MDI form before showing it.
            childForm.MdiParent = this;
            childFormNumber++;
            if (childFormNumber == 0)
            {
                childForm.Text = "Untitled Document";
            }
            else
            {
                childForm.Text = "Untitled Document " + childFormNumber;
            }
            childForm.Show(); 
            checkDocOpen();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenDocument();
        }
        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDocument document = new frmDocument();
            document.MdiParent=this;
            //create new SaveFiledialog with name saveFileDialog
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            //Set initial starting directory
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            //Set file filter
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|" +
                                    "CSS Templates (*.css)|*.css|" + 
                                    "HTML Templates (*.html)|*.html|" +
                                    "Theme Information (*.dat)|*.dat";
            //Set FilterIndex
            saveFileDialog.FilterIndex = 2;
            //Set dialog title
            saveFileDialog.Title = "Save ...";
            
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        void CutText()
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm.ContainsFocus)
                {
                    foreach (RichTextBox richTextBox in childForm.Controls)
                    {
                        richTextBox.Cut();
                    }
                }
            }
        }
        void CopyText()
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm.ContainsFocus)
                {
                    foreach (RichTextBox richTextBox in childForm.Controls)
                    {
                        richTextBox.Copy();
                    }
                }
            }
        }
        void PasteText()
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm.ContainsFocus)
                {
                    foreach (RichTextBox richTextBox in childForm.Controls)
                    {
                        richTextBox.Paste();
                    }
                }
            }
        }
        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CutText();
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CopyText();
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PasteText();
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Function not yet implemented.");
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            Focus();
            checkDocOpen();
        }
        private void checkDocOpen()
        {
            if (!docopen)
                this.closeToolStripMenuItem.Enabled = false;
            else
                this.closeToolStripMenuItem.Enabled = true;
        }
        private void OpenDocument()
        {    
            openFileDialog.Filter = "Text Files (*.txt)|*.txt|CSS Templates (*.css)|*.css|HTML Templates (*.html)|*.html|Theme Information (*.dat)|*.dat";
            openFileDialog.FilterIndex = 1;
            openFileDialog.Title = "Pick a file to open...";
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
          
            DialogResult dr = openFileDialog.ShowDialog();
            if (dr == DialogResult.OK)
            {
                // User click on Open
                // Open the file and get its content
                string txtFromFile = OpenFileAndReturnText(openFileDialog.FileName);

                // Instantiate our custom form and show it
                frmDocument editForm = new frmDocument();
                editForm.MdiParent = this;
                // Set the title of the Form
                editForm.Text = openFileDialog.FileName;
                // Set the contents of the Editor Form
                editForm.EditText = txtFromFile;
                editForm.Show();

            }
        }
        string OpenFileAndReturnText(string filename)
        {
            // Open the file given by param filname, read it 
            // and return content
            System.IO.StreamReader reader;
            try
            {
                reader = new System.IO.StreamReader(filename);
                return reader.ReadToEnd();
            }
            catch
            {
                MessageBox.Show("File could not be accessed");
                return "";
            }
        }
        protected void SaveActiveDocument()
        {
            // Get the active MDI child Form and cast it to the custom Form
            frmDocument activeForm = (frmDocument)this.ActiveMdiChild;

            if (activeForm != null)
            {
                // Instantiate the object
                SaveFileDialog saveDlg = new SaveFileDialog();
                // Set the suggested name
                saveDlg.FileName = activeForm.Text;
                saveDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                // Set filter, this also sets extension
                saveDlg.Filter = "Text Files (*.txt)|*.txt|CSS Templates (*.css)|*.css|HTML Templates (*.html)|*.html|Theme Information (*.dat)|*.dat";
                saveDlg.Title = "Save...";
                // Let the dialog set extension 
                saveDlg.AddExtension = true;


                if (saveDlg.ShowDialog() == DialogResult.OK)
                {
                    FileInfo fileinfo = new FileInfo(saveDlg.FileName);
                    StreamWriter streamwriter = fileinfo.CreateText();
                    streamwriter.Write(((frmDocument)this.ActiveMdiChild).EditText);
                    streamwriter.Close();
                    this.ActiveMdiChild.Text = saveDlg.FileName;
                }
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveActiveDocument();
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            SaveActiveDocument();
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm.ContainsFocus)
                {
                    foreach (RichTextBox richTextBox in childForm.Controls)
                    {
                        richTextBox.Undo();
                    }
                }
            }
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm.ContainsFocus)
                {
                    foreach (RichTextBox richTextBox in childForm.Controls)
                    {
                        richTextBox.Redo();
                    }
                }
            }
        }

        private void indexToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented yet.");
        }

        private void searchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not implemented yet.");
        }

        private void contentsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Help frmHelp = new Help();
            //frmHelp.MdiParent = this;
            frmHelp.Show();
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild.Name == "frmDocument")
            {
                document.Close();
            }
        }
    }
}
